package com.knight.tomcat.bio;

import com.knight.tomcat.common.RunClass;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class BIOTomcatServer {

    private static ExecutorService executorService = Executors.newCachedThreadPool();


    private int port;
    private String host;

    public BIOTomcatServer(int port, String host) {
        this.port = port;
        this.host = host;
    }

    public BIOTomcatServer() {
        this(8080, "localhost");
    }


    public void start() {
        //监听对应的端口
        try {
            ServerSocket serverSocket = new ServerSocket();
            serverSocket.bind(new InetSocketAddress(host, port));
            //获取客户端的输入
            System.out.println("tomcat 启动成功");
            while (true) {
                Socket socket = serverSocket.accept();
                //解析客户端的输入
                executorService.execute(new RunClass(socket));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
